<--- %%NOBANNER%% --> _prefix.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| List a list of variables using a prefix;                           |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Arguments:                                                         |
|    _prefix_ : a prefix, required;                                  |
|    _list_   : a list of suffix; or some looping command, such as   |
|               1 to 10 by 3 or 1 to 10 etc.                         |
|---------------<-- End of Files Arguments Needed-->-----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %put %_prefix(this, a b as);                              |
|          %put %_prefix(this, 2 to 10 by 5);                        | 
| Usage:   %_prefix(_prefix_, _list_)                                |
\-------------------<-- End of Files Created-->---------------------*/
%macro _prefix(_prefix_, _list_);
%if (%indexw(%nrbquote(%upcase(%nrbquote(&_list_))), %nrbquote(TO))) and 
    (((%words(%nrbquote(%upcase(%nrbquote(&_list_))), dlm=%nrbquote(TO BY%%)) eq 1) and  (%sysfunc(rxmatch(%sysfunc(rxparse($d*)),%nrbquote(%upcase(%nrbquote(&_list_)))))))  or 
     ((%words(%nrbquote(%upcase(%nrbquote(&_list_))), dlm=%nrbquote(TO BY%%)) eq 2) and  (%sysfunc(rxmatch(%sysfunc(rxparse($d* [' '] (TO) [' '] $d*)),%nrbquote(%upcase(%nrbquote(&_list_))))))) or 
     ((%words(%nrbquote(%upcase(%nrbquote(&_list_))), dlm=%nrbquote(TO BY%%)) eq 3) and  (%sysfunc(rxmatch(%sysfunc(rxparse($d* [' '] (TO) [' '] $d* [' '] (BY) [' '] $d*)),%nrbquote(%upcase(%nrbquote(&_list_)))))))) 
   %then %do;
   %let _prefixfrom_=; %let _prefixto_=; %let _prefixby_=1;
   %do _prefixi_ = 1 %to %words(%nrbquote(%upcase(%nrbquote(&_list_))), dlm=%nrbquote(TO BY%%));
      %if (&_prefixi_=1) %then 
         %let _prefixfrom_=%qscan(%nrbquote(%upcase(%nrbquote(&_list_))), &_prefixi_, %nrbquote(TO BY%%));
      %else %if (&_prefixi_=2) %then 
         %let _prefixto_=%qscan(%nrbquote(%upcase(%nrbquote(&_list_))), &_prefixi_, %nrbquote(TO BY%%));
      %else %if (&_prefixi_=3) %then 
         %let _prefixby_=%qscan(%nrbquote(%upcase(%nrbquote(&_list_))), &_prefixi_, %nrbquote(TO BY%%));
   %end;
   %if (%chk_type(&_prefixfrom_) eq 1) and (%chk_type(&_prefixto_) ne 1) and (%quote(&_prefixto_) ne) %then 
      %let _prefixto_=&_prefixfrom_;
   %else %if (%chk_type(&_prefixfrom_) ne 1) and (%chk_type(&_prefixto_) eq 1) and (%quote(&_prefixfrom_) ne) %then 
      %let _prefixfrom_=&_prefixto_;
   %if (%chk_type(&_prefixfrom_) eq 1) and (%chk_type(&_prefixto_) eq 1) and (%chk_type(&_prefixby_) ne 1) and (%quote(&_prefixby_) ne) %then
      %let _prefixby_=1;
   %let _prefixout_=;
   %if (%chk_type(&_prefixfrom_) ne 1) or (%chk_type(&_prefixto_) ne 1) or (%chk_type(&_prefixby_) ne 1) %then %do;
      %put ==> Alert! Invalid Input: at least one of from, to or by is a character variable!;
      %goto finish;
   %end; 
   %do _prefixi_=&_prefixfrom_ %to &_prefixto_ %by &_prefixby_;
      %let _prefixout_= &_prefixout_%str( )%trim(%left(&_prefix_))%trim(%left(&_prefixi_));
   %end; &_prefixout_
%end;
%else %do;
   %let _prefixout_ = %str() ;
   %do _prefixi_ = 1 %to %words(%nrbquote(&_list_));
      %let _prefixout_ = &_prefixout_%str( )%trim(%left(&_prefix_))%trim(%left(%qscan(%nrbquote(&_list_), &_prefixi_)));
   %end ;&_prefixout_
%end;
%finish:
%mend _prefix;